<?

class FormCreator
{
    private static $instance;
    
    private function __construct() {}
    
    public static function getInstance()
    {
        if (self::$instance === NULL)
        {
            self::$instance = new self;
        }
        return self::$instance;
    }
    
    /**
    * Vrati pole formularu pro editacni sablonu
    * @param array $cols Popis sloupcu tabulky
    * @param array $vals Hodnoty pro naplneni formulare
    * @return array Hodnoty radku
    * @access public
    * @author Adam Motvicka
    */
    public static function createTableForms($cols, $vals)
    {
        foreach ($cols as $c)
        {
            $array = $c;
            $array["value"] = $vals[$array["field"]];
            $forms[] = self::createTableForm($array);
        }
        return $forms;
    }
    
    /**
    * Zobrazi sablonu pro formularova data
    * @param array $forms Formularova data
    * @access public
    * @author Adam Motvicka
    */
    public static function printTableForms($forms)
    {
        include ("./templates/edit_row.php");
    }
    
    /**
    * Vytvori z describe sloupce formularove pole pro editaci
    * @param array $col DESCRIBE jednoho sloupce
    * @return array Instrukce pro vytvoreni jednoho formu
    * @access public
    * @author Adam Motvicka
    */
    public static function createTableForm($col)
    {
        $formtypes["input"] = array("varchar", "tinytext", "char", "tinyblob", "tinytext", "tinyint",
                                    "int", "date", "smallint", "mediumint", "bigint", "float", "decimal",
                                    "datetime", "timestamp", "time", "year");
        $formtypes["textarea"] = array("text", "blob", "mediumblob", "mediumtext", "longblob", "longtext",
                                       "binary", "varbinary");
        $formtypes["radio"] = array("set", "enum", "bool");
        
        if (in_array($col["content_type"], $formtypes["input"])) $col["input_type"] = "text";
        elseif (in_array($col["content_type"], $formtypes["textarea"])) $col["input_type"] = "textarea";
        elseif (in_array($col["content_type"], $formtypes["radio"])) $col["input_type"] = "radio";
        
        return $col;
    }
    
}

?>